package com.hzqy.commons.utils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.owasp.esapi.ESAPI;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;


public class HttpConectUtil {
	private static final Log LOG = LogFactory.getLog(HttpConectUtil.class);
	  
    public static String sendPost(String urlStr,int readTimeOut,int connectTimeout) {
		HttpURLConnection con = null;
		Map<String, String> map = new HashMap<String, String>();
		String result = "";
		InputStream is = null;
		InputStreamReader isr = null;
		BufferedReader br =null;
		if(UrlUtil.URL_LIST.contains(urlStr)) {
			try {// 获取HttpURLConnection连接对象
				URL url = new URL(urlStr);
				con = (HttpURLConnection) url.openConnection();
				// 设置连接属性
				con.setDoOutput(true);
				con.setRequestMethod("POST");
				con.setRequestProperty("version", "1.0");
				con.setRequestProperty("Content-Type", "text/xml");
				con.setRequestProperty("Pragma", "no-cache");
				con.setRequestProperty("Cache-Control", "no-cache");
				con.setConnectTimeout(connectTimeout);
				con.setReadTimeout(readTimeOut);

				int respCode = con.getResponseCode();
				if (respCode == 200) {
					is = con.getInputStream();
					isr = new InputStreamReader(is, "UTF-8");
					br = new BufferedReader(isr);
					String line = "";
					for (line = br.readLine(); line != null; line = br.readLine()) {
						result += line;
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
				LOG.error("请求失败", e);

			} finally {
				if (con != null)
					con.disconnect();
				if (is != null) {
					try {
						is.close();
					} catch (IOException e) {
					}
				}
				if (isr != null) {
					try {
						isr.close();
					} catch (IOException e) {
					}
				}
				if (br != null) {
					try {
						br.close();
					} catch (IOException e) {
					}
				}
			}
		}
		
		return result;
	}

	public static Map<String, Object> get(String urlStr) {
		PrintWriter writer = null;
		HttpURLConnection httpConn = null;
		Map<String, Object> result = new HashMap<String, Object>();
		String msg = "";
		String success = "0";

		if(!UrlUtil.URL_LIST.contains(urlStr)) {
			try {// 获取HttpURLConnection连接对象
				URL url = new URL(urlStr);
				httpConn = (HttpURLConnection) url.openConnection();
				// 设置连接属性
				httpConn.setConnectTimeout(20000);
				httpConn.setReadTimeout(30000);
				httpConn.setDoInput(true);
				httpConn.setDoOutput(true);
				httpConn.setUseCaches(false);
				httpConn.setRequestProperty("Connection", "Keep-Alive");
				//httpConn.setRequestProperty("Charset", CharSet);
				httpConn.setRequestProperty("Accept-Charset", "utf-8");
				httpConn.setRequestProperty("Content-Type", "text/html");
				httpConn.setRequestMethod("GET");


				int respCode = httpConn.getResponseCode();
				if (respCode == 200) {
					success = "1";
					msg = getStringFromInputStream(httpConn.getInputStream());
				} else {
					success = "2";
					msg = "response_status: " + respCode;
				}
			} catch (Exception e) {
				success = "2";
				//msg = e.getMessage();
			} finally {
				if (httpConn != null)
					httpConn.disconnect();
				close(writer);
			}


			result.put("msg", msg);
			result.put("success", success);
		}
		return result;
	}

	public static Map<String, Object> post(String urlStr) {
		PrintWriter writer = null;
		HttpURLConnection httpConn = null;
		Map<String, Object> result = new HashMap<String, Object>();
		String msg = "";
		String success = "0";

		if(!UrlUtil.URL_LIST.contains(urlStr)) {
			try {// 获取HttpURLConnection连接对象
				URL url = new URL(urlStr);
				httpConn = (HttpURLConnection) url.openConnection();
				// 设置连接属性
				httpConn.setConnectTimeout(20000);
				httpConn.setReadTimeout(30000);
				httpConn.setDoInput(true);
				httpConn.setDoOutput(true);
				httpConn.setUseCaches(false);
				httpConn.setRequestProperty("Connection", "Keep-Alive");

				httpConn.setRequestProperty("Accept-Charset", "UTF-8");
				//httpConn.setRequestProperty("Content-Type", "application/json");
				httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
				httpConn.setRequestMethod("POST");

				writer = new PrintWriter(httpConn.getOutputStream());
				writer.flush();

				int respCode = httpConn.getResponseCode();
				if (respCode == 200) {
					success = "1";
					msg = getStringFromInputStream(httpConn.getInputStream());
				} else {
					success = "2";
					msg = "response_status: " + respCode;
				}
			} catch (Exception e) {
				success = "2";
				//msg = e.getMessage();
			} finally {
				if (httpConn != null)
					httpConn.disconnect();
				close(writer);
			}


			result.put("msg", msg);
			result.put("success", success);
		}
		return result;
	}

	public static String getStringFromInputStream(InputStream inputStream) {
		StringBuffer str = new StringBuffer();
		BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
		try {
			String temp;
			while ((temp = br.readLine()) != null) {
				str.append(temp);
			}
		} catch (IOException e) {
		} finally {
			close(inputStream);
		}
		return str.toString();
	}

	private static final void close(Closeable closeable) {
		if (closeable != null) {
			try {
				closeable.close();
			} catch (IOException e) {
			}
		}
	}

    public static void main(String[] args) {
		System.out.println(ESAPI.encoder().encodeForHTML("<a href='sdfs'></a> < script > alert(); </ script >"));

	}

}
